<script setup lang="ts">
import Klotski from '~/composables/Klotski'
// import { useGameStore } from '~/stores/modules/gameState'
const games = reactive(Klotski)
const { randomData, level, isStart } = toRefs(games)

const handleMove = (index: number) => {
  games.move(index)
}
onKeyStroke(true, (e) => {
  e.preventDefault()
  if (!isStart.value)
    return
  games.onKeydown(e.keyCode)
})

games.init(3)
const changeGame = (number: number) => {
  games.init(number)
}

watch(() => games.status, (value) => {
  if (value === 'won') {
    ElMessage({
      message: `恭喜你，闯关成功，仅用${games.step}步`,
      type: 'success',
    })
  }
})
</script>

<template>
  <div>
    Klotski
  </div>
  <div flex="~ gap-10" justify-center p5>
    <el-button type="primary" @click="changeGame(3) ">
      Easy
    </el-button>
    <el-button type="primary" @click="changeGame(4)">
      Normal
    </el-button>
    <el-button type="primary" @click="changeGame(5)">
      Hard
    </el-button>
  </div>
  <div class="line">
    步数：{{ games.step }}
  </div>
  <div class="stage" mxa my4>
    <div class="content clearfix">
      <div
        v-for="item in randomData" :key="item" select-none :class="`img${level}`" min-w-8
        min-h-8
        m="0.5"
        border
        hover:bg-gray flex-c @click="handleMove(item)"
      >
        <div v-if="item !== randomData.length" style="font-size: 25px;">
          {{ item }}
        </div>
      </div>
    </div>
  </div>
  <Confetti :passed="games.status === 'won'" />
</template>

<style lang="scss" scoped>
.line {
    display: flex;
    align-items: center;
    justify-content: center;
    & + .line {
        margin-top: 20px;
    }
    .el-select {
        flex: 1;
    }
}
  .stage {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 500px;
    max-width: 100%;
    height: 500px;
    background-size: 100% 100%;
    overflow: hidden;
    .game-name {
      letter-spacing: 8px;
      font-size: 40px;
      font-weight: bold;
    }
    .content {
      overflow: hidden;
      > div {
        float: left;
        .el-image {
          width: 100%;
          height: 100%;
        }
      }
      .img3 {
        width: calc(475px / 3);
        height: calc(475px / 3);
        max-width: calc(90vw / 3);
        max-height: calc(90vw / 3);
      }
      .img4 {
        width: calc(475px / 4);
        height: calc(475px / 4);
        max-width: calc(90vw / 4);
        max-height: calc(90vw /4);
      }
      .img5 {
        width: calc(475px / 5);
        height: calc(475px / 5);
        max-width: calc(90vw / 5);
        max-height: calc(90vw / 5);
      }
    }
  }
  .other {
    width: 200px;
    margin-left: 20px;
    .point {
      width: 200px;
      height: 200px;
      padding: 16px 14px 16px 16px;
      background-size: 100% 100%;
    }

  }
</style>
